package com.wmlive.hhvideo.heihei.personal.presenter;

import android.text.TextUtils;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.personal.ReportTypeResponse;
import com.wmlive.hhvideo.heihei.beans.personal.UserHomeResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.hhvideo.heihei.personal.view.IOtherHomeView;
import com.wmlive.networklib.entity.BaseResponse;
import com.wmlive.networklib.entity.DCNetObserver;

import cn.wmlive.hhvideo.R;

/**
 * Created by XueFei on 2017/5/31.
 * <p>
 * 他人主页
 */

public class OtherHomePresenter extends BasePresenter<IOtherHomeView> {
    public OtherHomePresenter(IOtherHomeView view) {
        super(view);
    }

    /**
     * 获取他人信息
     * @param user_id
     */
    public void getOtherHomeInfo(long user_id) {
        String url = InitCatchData.userUserHome();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_PERSONAL_OTHER_HOME, getHttpApi().getUserHome(url, user_id))
                    .subscribe(new DCNetObserver<UserHomeResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, UserHomeResponse response) {
                            if (null != viewCallback) {
                                viewCallback.handleOtherHomeInfoSucceed(response.getUser_info());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleOtherHomeInfoFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }

    /**
     * 是否关注
     * @param isFollow
     * @param user_id
     */
    public void updateFocusState(final boolean isFollow, long user_id) {
        String url = "";
        int requstCode = 0;
        if (isFollow) {
            url = InitCatchData.userUnFollowUsr();
            requstCode = HttpConstant.TYPE_PERSONAL_FOCUS_UNFOLLOW;
        } else {
            url = InitCatchData.userFollowUser();
            requstCode = HttpConstant.TYPE_PERSONAL_FOCUS_FOLLOW;
        }

        if (!TextUtils.isEmpty(url)) {
            executeRequest(requstCode, getHttpApi().updateFollowerState(url, user_id))
                    .subscribe(new DCNetObserver<BaseResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, BaseResponse response) {
                            if (null != viewCallback) {
                                if (!isFollow) {
                                    viewCallback.handleFocusSucceed(true, R.string.user_follower);
                                } else {
                                    viewCallback.handleFocusSucceed(false, R.string.user_unfollower);
                                }
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleFocusFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }

    /**
     * 获取举报内容
     */
    public void getReportList() {
        String url = InitCatchData.userListReportType();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_PERSONAL_REPORT_LIST, getHttpApi().getListReportType(url))
                    .subscribe(new DCNetObserver<ReportTypeResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ReportTypeResponse response) {
                            if (null != viewCallback) {
                                viewCallback.handleReportListSucceed(response);
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleReportListFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }

    /**
     * 举报
     * @param user_id
     * @param report_type_id
     */
    public void reportUser(long user_id, int report_type_id) {
        String url = InitCatchData.userReport();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_PERSONAL_REPORT, getHttpApi().reportUser(url, user_id,report_type_id))
                    .subscribe(new DCNetObserver<BaseResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, BaseResponse response) {
                            if (null != viewCallback) {
                                viewCallback.handleReportSucceed();
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleReportFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }

}
